package databasetest;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class Selete extends JInternalFrame {
	Connection conn;
	DefaultTableModel model;
	Vector<String> col = new Vector<String>();
	private JButton btnNewButton;

	private JTextField textField;
	private JScrollPane scrollPane;
	private JTable table_1;
	Vector<String> vex;
	DatabaseTest d;
	Update su;
	/**
	 * Create the frame.
	 */
	public Selete(Connection c, DatabaseTest d) {

		super("조회", true, true, true, false);
		this.d = d;
		setBounds(100, 100, 929, 592);
		this.conn = c;
		model = new DefaultTableModel(col, 0) {
			public boolean isCellEditable(int rowIndex, int mColIndex) {
				return false;
			}
		};
		getContentPane().setLayout(null);
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getTextField());
		getContentPane().add(getScrollPane_1());

	}
	
	public void setList(){
		String s = textField.getText();
		try{
			PreparedStatement ptms = conn.prepareStatement(s);
			ResultSet rs = ptms.executeQuery();
			ResultSetMetaData rsm = rs.getMetaData();
			Vector v = new Vector();
			v.add("NO");
			for(int i=1; i<rsm.getColumnCount()+1; i++){
				v.add(rsm.getColumnName(i));
					
			}
			
			model = new DefaultTableModel(v,0);
			table_1.setModel(model);
			int cnt = 1;
			while(rs.next()){
				Vector d = new Vector<>();
				d.add(cnt);
				for(int i=1; i<rsm.getColumnCount()+1;i++){
					d.add(rs.getString(i));
				}
				model.addRow(d);
				cnt++;
			}
			
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	public void search() {
		try {
			if (model != null) {
				model.setRowCount(0);
				model.setColumnCount(0);
			}

			String sql = textField.getText();
			Statement pmtst = conn.createStatement();
			ResultSet rs = pmtst.executeQuery(sql);
			ResultSetMetaData rsmd = rs.getMetaData();
			int colCount = rsmd.getColumnCount();
			// int colRow = rsmd.
			// vector
			for (int i = 1; i <= colCount; i++) {
				model.addColumn(rsmd.getColumnName(i));
//				System.out.println(i);
			}

			scrollPane.setViewportView(getTable_1());

			while (rs.next()) {
				Vector<String> row = new Vector<String>();
				for (int i = 1; i <= colCount; i++) {
					row.add(rs.getString(i));
				}
				model.addRow(row);

			}
			rs.close();
			pmtst.close();

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void mouseC() {
		int index = table_1.getSelectedRow();
		Vector<String> vx = new Vector<String>();
		vx.add((String) table_1.getValueAt(index, 0));
		vx.add((String) table_1.getValueAt(index, 1));
		vx.add((String) table_1.getValueAt(index, 2));
		if (su == null) {
			su = new Update(conn,d);
			d.contentPane.add(su);
			su.setVisible(true);
		}
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("조회");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
//					search();
					setList();
				}
			});
			btnNewButton.setBounds(308, 20, 97, 23);
		}
		return btnNewButton;
	}



	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent a) {
					if (a.getKeyCode() == KeyEvent.VK_ENTER) {
//						search();
						setList();
					}

				}
			});
			textField.setBounds(10, 20, 286, 23);
			textField.setColumns(10);
		}
		return textField;
	}

	private JScrollPane getScrollPane_1() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 63, 889, 490);
			scrollPane.setViewportView(getTable_1());
		}
		return scrollPane;
	}

	private JTable getTable_1() {
		if (table_1 == null) {
			table_1 = new JTable(model);
			table_1.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent ev) {
//					mouseC();
					int row = table_1.getSelectedRow();
					String s = (String)table_1.getValueAt(row, 3);
					String c = (String)table_1.getValueAt(row, 1);
					String e = (String)table_1.getValueAt(row, 2);
					if(d.update == null){
						
					   d.openUpdate();
					}
						d.update.setData(s, c, e);
						d.update.setVisible(true);
						d.update.toFront();
						d.insert.setInsert(c,e);
						d.insert.toFront();
						d.delete.setDelete(s,c,e);
						d.delete.toFront();
				}
			});
		}
		return table_1;
	}
}
